﻿using PubSubGeneric.WCFContracts;
using PubSubGeneric.WCFContracts.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace PubSubGeneric.WCFPublishingServer
{
    public class WCFPublishingServiceAdapter : IWCFPublishingServiceAdapter
    {
        IPublishingService PublishingService;
        MethodInfo PublishMI;

        public WCFPublishingServiceAdapter()
        {
            PublishingService = new PublishingService();
            PublishMI = typeof(PublishingService).GetMethod("Publish");
        }

        public void Publish(string topic, string arg, string typeName)
        {
            MethodInfo GenericPublishMI = PublishMI.MakeGenericMethod(Type.GetType(typeName));

            object argobj = WCFSerializer.DeSerialize(arg, typeName);

            GenericPublishMI.Invoke(PublishingService, new object[] { topic, argobj });
        }

    }
}
